/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.poker.handler;

import log.Debug;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.game.poker.PokerConversion;
import extension.game.poker.PokerFlag;
import extension.game.poker.PokerGame;
import extension.game.poker.PokerPlayer;
import extension.game.poker.PokerRoomExtension;
import extension.game.poker.RoomResponse;

/**
 *
 * @author daz
 */
public class BettingActionHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        PokerRoomExtension pokerRoomExtension = (PokerRoomExtension) getParentExtension();
        PokerGame pokerGame = pokerRoomExtension.getPokerGame();
        RoomResponse response = pokerRoomExtension.getResponse();
        //info receive
        int bettingChoice = params.getInt(PokerFlag.BETTING_CHOICE);
        Object object = params.getInt(PokerFlag.BETTING_MONEY);
        long bettingMoney = 0l;
        if (object != null) {
            try {
                bettingMoney = (Long) object;
            } catch (Exception ex) {
                  bettingMoney = (Integer) object;
            }
            int bettingPlayer = pokerGame.getBettingTurn();
            Debug.d(user.getName() + " betting choice:" + bettingChoice + ",betting money :" + bettingMoney + ",betting player :" + bettingPlayer);
            PokerPlayer pokerPlayer = pokerGame.getListPlayers()[bettingPlayer];
            pokerPlayer.setBettingChoice(bettingChoice);
            pokerGame.betAction();
            switch (bettingChoice) {
                case PokerGame.FOLD:
                    pokerGame.checkFinishWhenFold(bettingPlayer);
                    bettingChoice = PokerGame.FOLD;
                    bettingMoney = 0l;
                    break;
                case PokerGame.CHECK:
                    bettingChoice = PokerGame.CHECK;
                    bettingMoney = 0l;
                    break;

                case PokerGame.CALL:
                    long moneyMustBet = pokerGame.getMoneyMustBet();
                    bettingMoney = moneyMustBet - pokerPlayer.getBet();
                    bettingChoice = PokerGame.CALL;
                    if (bettingMoney > pokerPlayer.getMoney()) {
                        bettingMoney = pokerPlayer.getMoney();
                    }
                    Debug.d("contribute " + pokerPlayer.getName() + ":" + bettingMoney);
                    pokerGame.contributePot(bettingMoney, bettingPlayer);
                    pokerPlayer.betMore(bettingMoney);
                    Debug.d(pokerPlayer.getName() + " money :" + pokerPlayer.getMoney());
                    if (pokerPlayer.getMoney() <= 0) {
                        pokerGame.checkFinishWhenAllBet(bettingPlayer);
                    }
                    break;

                case PokerGame.RAISE:
                    moneyMustBet = pokerGame.getMoneyMustBet();
                    if (moneyMustBet == 0) {
                        bettingChoice = PokerGame.BET;
                    }
                    bettingMoney = (bettingMoney > pokerPlayer.getMoney() ? pokerPlayer.getMoney() : bettingMoney);
                    pokerGame.setMustBet(bettingMoney + pokerPlayer.getBet());
                    pokerPlayer.betMore(bettingMoney);
                    Debug.d("contribute " + pokerPlayer.getName() + ":" + bettingMoney);
                    pokerGame.contributePot(bettingMoney, bettingPlayer);
                    Debug.d(pokerPlayer.getName() + " money :" + pokerPlayer.getMoney());
                    if (pokerPlayer.getMoney() <= 0) {
                        pokerGame.checkFinishWhenAllBet(bettingPlayer);
                    }
                    break;
            }

            response.sendBettingAction(bettingChoice, bettingMoney, bettingPlayer, pokerPlayer.getMoney(), pokerRoomExtension.getParentRoom().getUserList());
            logBettingInfo(bettingChoice, bettingMoney, pokerPlayer.getName());
            if (bettingChoice >= 2) {
                pokerRoomExtension.getResponse().sendMessageChat(user.getName() + PokerConversion.getBetActionName(bettingChoice) + " " + bettingMoney, 0);
            } else {
                pokerRoomExtension.getResponse().sendMessageChat(user.getName() + PokerConversion.getBetActionName(bettingChoice), 0);
            }
            pokerGame.nextTurn();
        } else {
            int bettingPlayer = pokerGame.getBettingTurn();
            PokerPlayer pokerPlayer = pokerGame.getListPlayers()[bettingPlayer];
            pokerPlayer.setBettingChoice(PokerGame.FOLD);
            pokerGame.betAction();
            pokerGame.checkFinishWhenFold(bettingPlayer);
            bettingChoice = PokerGame.FOLD;
            bettingMoney = 0l;

            response.sendBettingAction(bettingChoice, bettingMoney, bettingPlayer, pokerPlayer.getMoney(), pokerRoomExtension.getParentRoom().getUserList());
            logBettingInfo(bettingChoice, bettingMoney, pokerPlayer.getName());
            pokerGame.nextTurn();
        }
    }

    private void logBettingInfo(int bettingChoice, long bettingMoney, String playerName) {
        Debug.i(playerName + "-" + PokerConversion.getBetActionName(bettingChoice) + ",B=" + bettingMoney);
    }
}
